package com.cp.weixin.blog.controller;

import com.cp.weixin.blog.repository.Record;
import com.cp.weixin.blog.service.CategoryService;
import com.cp.weixin.blog.service.RecordService;
import com.cp.weixin.blog.service.TagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Controller
public class BlogController extends BaseController {

    Logger log = LogManager.getLogger();

    @Autowired
    RecordService recordService;

    @Autowired
    TagService tagService;

    @Autowired
    CategoryService categoryService;

    @RequestMapping(value = {"/blog.html", "/blog", "/blog-list.html"})
    public String getBlogList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "categoryId", defaultValue = "0") Integer categoryId, @RequestParam(name = "tagId", defaultValue = "0") Integer tagId, Model model) {
        int state = 1;
        PageHelper.startPage(pageNum, 10);
        PageInfo<Record> recordPageInfo = new PageInfo<Record>(recordService.getRecordByPageAndStateAndCategoryId(state, categoryId, tagId));
        model.addAttribute("recordPageInfo", recordPageInfo);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("tagId", tagId);
        return "blog/blogList.html";
    }

    @RequestMapping(value = {"/all-blog.html"})
    public String getAllBlogList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, Model model) {
        int state = 0;
        int categoryId = 0;
        int tagId = 0;
        PageHelper.startPage(pageNum, 10);
        PageInfo<Record> recordPageInfo = new PageInfo<Record>(recordService.getRecordByPageAndStateAndCategoryId(state, categoryId, tagId));
        model.addAttribute("recordPageInfo", recordPageInfo);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("tagId", tagId);
        return "blog/blogList.html";
    }

    @GetMapping("/blog/{uid}.html")
    public String getBlog(@PathVariable(name = "uid") String uid, Model model) {
        Record record = recordService.getRecordByUid(uid);

        //阅读数量+1
        try {
            recordService.readCountPlus(record.getUid());
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        model.addAttribute("record", record);
        return "blog/blogItem.html";
    }

    @GetMapping("/blog/edit-{uid}.html")
    public String editBlog(@PathVariable(name = "uid") String uid, Model model) {
        Record record = recordService.getRecordByUid(uid);
        model.addAttribute("record", record);
        return "blog/editItem.html";
    }

    @RequestMapping(value = "/blog/updateBlog", method = RequestMethod.POST)
    public String updateBlog(Record record, Integer categoryId, BindingResult bindingResult) {
        int result = 0;
        //默认修改后自动发布
        result = recordService.updateAndPublishRecord(record, categoryId);
        log.warn("update blog : " + result + ", "+ record.getTitle());
        return "redirect:/blog/"+record.getUid()+".html";
    }

    @RequestMapping(value = "/addBlog")
    public String addBlog(Model model) {
        return "blog/addBlog.html";
    }

    @RequestMapping(value = "/saveBlog")
    public String saveBlog(Record record, Integer categoryId, Model model) {
        String uid = UUID.randomUUID().toString();
        record.setUid(uid);
        int flag = recordService.addRecord(record, categoryId);
        log.warn("add blog:"+flag);
        return "redirect:/blog/"+uid+".html";
    }

}
